package com.ktgj.train.task;

import com.ktgj.common.core.constant.CustomerConstants;
import com.ktgj.common.core.constant.FlightOrderConstants;
import com.ktgj.common.core.enums.TrainOrderEnum;
import com.ktgj.train.domain.*;
import com.ktgj.train.mapper.TrainOrderMapper;
import com.ktgj.train.mapper.TrainOrderOperationMapper;
import com.ktgj.train.mapper.TrainOrderRefundrecordMapper;
import com.ktgj.train.mapper.TrainOrderitemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class Scheduler {
    @Autowired
    private TrainOrderMapper orderMapper;
    @Autowired
    private TrainOrderitemMapper orderitemMapper;
    @Autowired
    private TrainOrderRefundrecordMapper trainOrderRefundrecordMapper;
    @Autowired
    private TrainOrderOperationMapper trainOrderOperationMapper;

    /**
     * 取消火车票订单
     */
    @Scheduled(fixedDelay = 1000 * 60)
    public void cancelTrainOrder() {
        List<TrainOrder> trainOrders = orderMapper.queryTrainOrders();
        for (TrainOrder trainOrder : trainOrders) {
            trainOrder.setPaymentorderstatus(TrainOrderEnum.cancelOrder.getValue());
            trainOrder.setStatus(TrainOrderEnum.cancelOrder.getValue());
            orderMapper.updateTrainOrder(trainOrder);
            TrainOrderitem orderitem = new TrainOrderitem();
            orderitem.setOrderid(trainOrder.getOrderid());
            orderitem.setStatus(TrainOrderEnum.cancelOrder.getValue());
            orderitemMapper.updateTrainOrderitems(orderitem);

            TrainOrderOperation orderOperation = new TrainOrderOperation();
            orderOperation.setOperationname("取消订单");
            orderOperation.setMemo("超时未支付");
            orderOperation.setOrderid(trainOrder.getOrderid());
            orderOperation.setOpemployeeid(1L);
            orderOperation.setOpemployeename("系统服务");
            orderOperation.setCreateTime(new Date());
            trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
        }
    }

    /**
     * 火车票改签支付（超出支付时间将订单状态改回未进行改签时的状态，之后此订单无法进行改签操作）
     * 每一分钟执行一次
     * */
    @Scheduled(cron = "0 */1 * * * ?")
    public void rebookPayTrainOrder(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //获取改签未支付订单集合且当前时间》创建时间+10分钟
        List<TrainOrderRefundrecord> trainOrderRefundrecords = trainOrderRefundrecordMapper.rebookPayTrainOrder();
        if (trainOrderRefundrecords.size() > 0){
            for (TrainOrderRefundrecord trainOrderRefundrecord : trainOrderRefundrecords) {
                //获取改签订单对应的主订单
                TrainOrder trainOrder = orderMapper.selectTrainOrderById(Long.valueOf(trainOrderRefundrecord.getOrderid()));
                TrainOrderOperation orderOperation = new TrainOrderOperation();
                orderOperation.setOperationname("取消订单");
                orderOperation.setMemo("超时未支付");
                orderOperation.setOrderid(trainOrder.getOrderid());
                orderOperation.setOpemployeeid(1L);
                orderOperation.setOpemployeename("系统服务");
                orderOperation.setCreateTime(new Date());
                trainOrderOperationMapper.insertTrainOrderOperation(orderOperation);
                //拼接出发时间
                String time = trainOrder.getTraveldate() + " " + trainOrder.getDeparturetime();
                Date date = new Date();
                try {
                    Date departureTime = format.parse(time);
                    //判断当前时间小于出发时间
                    if (date.compareTo(departureTime) < 0){
                        trainOrderRefundrecordMapper.deleteTrainOrderRefundrecordById(trainOrderRefundrecord.getRecordid());

                        TrainOrder tmpTrainOrder = new TrainOrder();
                        tmpTrainOrder.setOrderid(trainOrder.getOrderid());
                        tmpTrainOrder.setStatus(FlightOrderConstants.Ticketing.getValue());
                        int i = orderMapper.updateTrainOrder(tmpTrainOrder);
                        if (i <= 0){
                            System.out.println("修改失败");
                        }
                    }
                } catch (Exception e){
                    System.out.println("日期转换错误");
                }
            }
        }
    }
}
